/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.auth.sys.service.impl;

import com.arisgo.cloud.auth.sys.entity.RoleMenu;
import com.arisgo.cloud.auth.sys.mapper.RoleMenuMapper;
import com.arisgo.cloud.auth.sys.service.RoleMenuService;
import com.arisgo.cloud.auth.sys.vo.FunctionMenuVo;
import com.arisgo.cloud.auth.sys.vo.RoleMenuVo;
import com.arisgo.cloud.common.utils.AssertUtil;
import com.arisgo.cloud.web.mybatis.service.CloudServiceImpl;
import com.arisgo.cloud.web.param.Param;
import com.arisgo.cloud.web.param.Search;
import com.arisgo.cloud.web.utils.RequestUtil;
import com.arisgo.cloud.web.utils.TreeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色菜单
 *
 * @author Arisgo
 * @since 2024-06-14
 */
@Service
public class RoleMenuServiceImpl extends CloudServiceImpl<RoleMenuMapper, RoleMenu, RoleMenuVo> implements RoleMenuService {

    @Override
    public RoleMenu getInstanceModel() {
        return new RoleMenu();
    }

    @Override
    public RoleMenuVo getInstanceVo() {
        return new RoleMenuVo();
    }

    @Override
    public List<FunctionMenuVo> treeMenuByRoleId(Long roleId) {
        Long tenantId = RequestUtil.getTenantId();
        List<FunctionMenuVo> menus = mapper.treeMenuByRoleId(tenantId, roleId);
        return TreeUtil.convertTree(menus);
    }

    @Override
    @Transactional
    public Boolean save(Long roleId, List<Long> menuIds) {
        AssertUtil.notNull(roleId, "roleId不能为空");
        mapper.deleteByQuery(Param.creator(RoleMenu.ROLE_ID, roleId).buildWrapper());
        if (CollectionUtils.isEmpty(menuIds)) {
            return true;
        }
        List<RoleMenu> data = new ArrayList<>(menuIds.size());
        for (Long menuId : menuIds) {
            RoleMenu menu = new RoleMenu();
            menu.setRoleId(roleId);
            menu.setMenuId(menuId);
            data.add(menu);
        }
        return mapper.insertBatch(data) > 0;
    }

    @Override
    @Transactional
    public Boolean remove(Long roleId, List<Long> menuIds) {
        Param role = Param.creator(RoleMenu.ROLE_ID, roleId);
        Param menu = Param.creator(RoleMenu.MENU_ID, "IN", menuIds);
        Search search = Search.creator(role, menu);
        mapper.deleteByQuery(search.buildWrapper());
        return true;
    }
}
